﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Commons;
using eTMS.Controllers;
using eTMS.Controllers.SinhVienTrungTuyenService;
using eTMS.Controllers.HoSoChiTietService;

namespace eTMS.Forms.Students
{
    public partial class FrmNhapTruong : eTMS.Controls.BaseForm
    {
        public FrmNhapTruong()
        {
            InitializeComponent();
        }
        SinhVienTrungTuyenController sinhVienTrungTuyenController = new SinhVienTrungTuyenController();
        SinhVienController sinhVienController = new SinhVienController();
        private void LoadLanguage()
        {
            this.ColumnSTT.HeaderText = Constants.APP_INDEX;
            ColumnSoBaoDanh.HeaderText = SinhVienTrungTuyenStringMapping.SoBaoDanh;
            ColumnHoTenSinhVien.HeaderText = SinhVienTrungTuyenStringMapping.HoTenSinhVien;
            ColumnGioiTinh.HeaderText = SinhVienTrungTuyenStringMapping.GioiTinh;
            ColumnNgaySinh.HeaderText = SinhVienTrungTuyenStringMapping.NgaySinh;
            //Search
            this.groupTimHiem.Text = Constants.APP_SEARCH;
            this.groupBoxFilter.Text = Constants.APP_SEARCH_ADVANDCE;
            labelSoBaoDanhFilter.Text = SinhVienTrungTuyenStringMapping.SoBaoDanh;
            cbkHoTenSinhVienFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelHoTenSinhVienFilter.Text = SinhVienTrungTuyenStringMapping.HoTenSinhVien;
            cbkDiaChiFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelDiaChiFilter.Text = SinhVienTrungTuyenStringMapping.DiaChi;
            cbkSoCMNDFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelSoCMNDFilter.Text = SinhVienTrungTuyenStringMapping.SoCMND;
            cbkNgaySinhFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelNgaySinhFilter.Text = SinhVienTrungTuyenStringMapping.NgaySinh;
            cbkDanTocFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelDanTocFilter.Text = SinhVienTrungTuyenStringMapping.DanToc;
            cbkTonGiaoFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelTonGiaoFilter.Text = SinhVienTrungTuyenStringMapping.TonGiao;
            cbkQuocGiaFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelQuocGiaFilter.Text = SinhVienTrungTuyenStringMapping.QuocGia;
            labelNamTNPTFilter.Text = SinhVienTrungTuyenStringMapping.NamTNPT;
            cbkNamTNPTFilter.Text = Constants.APP_SEARCH;
            cbkDoiTuongFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelDoiTuongFilter.Text = SinhVienTrungTuyenStringMapping.DoiTuong;
            cbkKhuVucFilterExact.Text = Constants.APP_SEARCH_EXACT;
            labelKhuVucFilter.Text = SinhVienTrungTuyenStringMapping.KhuVuc;
            txtIsDaNopFilter.Items.Add(Constants.APP_ALL);
            txtIsDaNopFilter.Items.Add(Constants.APP_TRUE);
            txtIsDaNopFilter.Items.Add(Constants.APP_FALSE);
            //txtIsDaNopFilter.SelectedIndex = 2;
            labelIsDaNopFilter.Text = SinhVienTrungTuyenStringMapping.IsDaNop;
            labelKhoaHoc_IDFilter.Text = SinhVienTrungTuyenStringMapping.KhoaHoc_ID;
            //end
            labelNamHoc.Text = NamHocStringMapping.NamHoc;
            labelHocKy.Text = HocKyStringMapping.HocKy;
        }
        private void FrmNhapTruong_Load(object sender, EventArgs e)
        {
            groupBoxFilter.ToggleCollapsed();
            LoadLanguage();
            HoSoController.LoadAllHoSo(false);
            KhoaHocController.LoadAllKhoaHoc(false);
            NamHocController.LoadAllNamHoc(false);
            HocKyController.LoadAllHocKy(false);
            comboBoxNamHoc1.BindData();
            comboBoxHocKy1.BindData();
            txtKhoaHoc_IDFilter.BindData();
            txtKhoaHoc_IDFilter.SelectedIndex = txtKhoaHoc_IDFilter.Items.Count - 1;
            sinhVienInfoInput1.SetController(sinhVienController);
            LoadLoaiGiayTo();
            KhoanTienNhapTruongController khoanTienNhapTruongController = new KhoanTienNhapTruongController();
            khoanTienNhapTruongController.InitTable();
            sinhVienPayment1.SetDataSource(khoanTienNhapTruongController.Table);
            sinhVienPayment1.SetController(sinhVienController);
            lyLichSinhVienInfo1.SetController(sinhVienController);
        }

        private List<HoSoChiTietEntity> listHoSoNhapHoc;
        private void LoadLoaiGiayTo()
        {
            loaiGiayToCheckList1.AddColumnSoLuong();
            loaiGiayToCheckList1.AddColumnGhiChu();
            HoSoController hoSoController = new HoSoController();
            listHoSoNhapHoc = hoSoController.LoadAllHoSoChiTietNhapHoc();
            InitGiayToNhapHoc();
        }
        private void InitGiayToNhapHoc()
        {
            List<int> ls = listHoSoNhapHoc.ConvertAll<int>(delegate(HoSoChiTietEntity oConvert) { return oConvert.LoaiGiayTo_ID; });
            loaiGiayToCheckList1.ListLoaiGiayToVisibled = ls;
            loaiGiayToCheckList1.BindData();
            foreach (HoSoChiTietEntity entity in listHoSoNhapHoc)
            {
                loaiGiayToCheckList1.SetSoLuongLoaiGiayTo(entity.LoaiGiayTo_ID, entity.SoLuongYeuCau);
            }

        }
        private string GetFilter()
        {
            string strFilter = "";
            if (txtSoBaoDanhFilter.Text.Trim() != "")
            {
                strFilter += " AND SoBaoDanh='" + txtSoBaoDanhFilter.Text.Trim() + "'";
            }
            if (txtHoTenSinhVienFilter.Text.Trim() != "")
            {
                if (cbkHoTenSinhVienFilterExact.Checked)
                {
                    strFilter += " AND HoTenSinhVien='" + txtHoTenSinhVienFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND HoTenSinhVien LIKE '%" + txtHoTenSinhVienFilter.Text.Trim() + "%'";
                }
            }
            if (txtDiaChiFilter.Text.Trim() != "")
            {
                if (cbkDiaChiFilterExact.Checked)
                {
                    strFilter += " AND DiaChi='" + txtDiaChiFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND DiaChi LIKE '%" + txtDiaChiFilter.Text.Trim() + "%'";
                }
            }
            if (txtSoCMNDFilter.Text.Trim() != "")
            {
                if (cbkSoCMNDFilterExact.Checked)
                {
                    strFilter += " AND SoCMND='" + txtSoCMNDFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND SoCMND LIKE '%" + txtSoCMNDFilter.Text.Trim() + "%'";
                }
            }

            if (txtNgaySinhFilter.Text.Trim() != "")
            {
                if (cbkNgaySinhFilterExact.Checked)
                {
                    strFilter += " AND NgaySinh='" + txtNgaySinhFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND NgaySinh LIKE '%" + txtNgaySinhFilter.Text.Trim() + "%'";
                }
            }

            if (txtDanTocFilter.Text.Trim() != "")
            {
                if (cbkDanTocFilterExact.Checked)
                {
                    strFilter += " AND DanToc='" + txtDanTocFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND DanToc LIKE '%" + txtDanTocFilter.Text.Trim() + "%'";
                }
            }
            if (txtTonGiaoFilter.Text.Trim() != "")
            {
                if (cbkTonGiaoFilterExact.Checked)
                {
                    strFilter += " AND TonGiao='" + txtTonGiaoFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND TonGiao LIKE '%" + txtTonGiaoFilter.Text.Trim() + "%'";
                }
            }
            if (txtQuocGiaFilter.Text.Trim() != "")
            {
                if (cbkQuocGiaFilterExact.Checked)
                {
                    strFilter += " AND QuocGia='" + txtQuocGiaFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND QuocGia LIKE '%" + txtQuocGiaFilter.Text.Trim() + "%'";
                }
            }
            if (cbkNamTNPTFilter.Checked)
            {
                strFilter += " AND NamTNPT=" + txtNamTNPTFilter.Value.ToString();
            }
            if (txtDoiTuongFilter.Text.Trim() != "")
            {
                if (cbkDoiTuongFilterExact.Checked)
                {
                    strFilter += " AND DoiTuong='" + txtDoiTuongFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND DoiTuong LIKE '%" + txtDoiTuongFilter.Text.Trim() + "%'";
                }
            }
            if (txtKhuVucFilter.Text.Trim() != "")
            {
                if (cbkKhuVucFilterExact.Checked)
                {
                    strFilter += " AND KhuVuc='" + txtKhuVucFilter.Text.Trim() + "'";
                }
                else
                {
                    strFilter += " AND KhuVuc LIKE '%" + txtKhuVucFilter.Text.Trim() + "%'";
                }
            }

            if (txtIsDaNopFilter.SelectedIndex > 0)
            {
                strFilter += " AND IsDaNop=" + (txtIsDaNopFilter.SelectedIndex == 1 ? "1" : "0");
            }
            if (strFilter.Length > 0)
                strFilter = strFilter.Substring(4);
            return strFilter;
        }
        private void FilterData()
        {
            if (sinhVienTrungTuyenController.Table == null)
                LoadDataSinhVienTrungTuyen();
            sinhVienTrungTuyenController.Table.DefaultView.RowFilter = GetFilter();
            gridData.AutoGenerateColumns = false;
            gridData.DataSource = sinhVienTrungTuyenController.Table;
            if (gridData.Rows.Count > 0)
            {
                gridData.ClearSelection();
                gridData.Rows[0].Selected = true;
            }
        }
        private void LoadDataSinhVienTrungTuyen()
        {
            sinhVienTrungTuyenController.LoadAllDSDataFilter((int)txtKhoaHoc_IDFilter.SelectedValue, "");
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadDataSinhVienTrungTuyen();
            FilterData();
        }

        private void txtSoBaoDanhFilter_TextChanged(object sender, EventArgs e)
        {
            FilterData();
        }

        private void txtSoBaoDanhFilter_OnPressEnter(object sender, EventArgs e)
        {
            if (gridData.SelectedRows.Count > 0)
            {
                btnChonSinhVien.PerformClick();
            }
            else
            {
                btnSearch.PerformClick();
            }
        }
        private SinhVienTrungTuyenEntity GetObject()
        {
            SinhVienTrungTuyenEntity o = sinhVienTrungTuyenController.MapDataRowToObject(SelectedDataRow());
            sinhVienTrungTuyenController.SinhVienTrungTuyenObject = o;
            return o;
        }
        private DataRow SelectedDataRow()
        {
            return ((DataRowView)gridData.SelectedRows[0].DataBoundItem).Row;
        }
        private void btnChonSinhVien_Click(object sender, EventArgs e)
        {
            SinhVienTrungTuyenEntity oTrungTuyen = GetObject();
            if (oTrungTuyen.SinhVien_ID > 0)
            {
                sinhVienController.LoadObject(oTrungTuyen.SinhVien_ID);
                sinhVienController.LoadAllNganhHoc();
                sinhVienController.LoadLyLichSinhVien();
                if (sinhVienController.LsNganhHoc.Count > 0)
                    chonNganhHoc1.SetDefaultNganhHoc(sinhVienController.LsNganhHoc[0].MaNganhHoc);
                sinhVienController.LoadSoCoSinhVien((int)comboBoxHocKy1.SelectedValue, (int)comboBoxNamHoc1.SelectedValue);
                sinhVienPayment1.UpdateToScreen();
                lyLichSinhVienInfo1.UpdateToScreen();
            }
            else
            {
                sinhVienController.ResetLyLichSinhVien();
                sinhVienController.ResetObject();
                sinhVienController.SinhVienObject.DiaChi = oTrungTuyen.DiaChi.Trim();
                sinhVienController.SinhVienObject.SoCMND = oTrungTuyen.SoCMND.Trim();
                sinhVienController.SinhVienObject.TenSinhVien = oTrungTuyen.HoTenSinhVien.Trim();
                sinhVienController.SinhVienObject.NgaySinh = oTrungTuyen.NgaySinh.Trim();
                sinhVienController.SinhVienObject.GioiTinh = (byte)(oTrungTuyen.GioiTinh ? 1 : 0);
                sinhVienController.SinhVienObject.DienThoai = oTrungTuyen.DienThoai.Trim();
                sinhVienController.SinhVienObject.Email = "";
                chonNganhHoc1.SetDefaultNganhHoc(oTrungTuyen.NganhTrungTuyen);
                lyLichSinhVienInfo1.UpdateToScreen();
            }
            sinhVienInfoInput1.UpdateToScreen();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            sinhVienInfoInput1.UpdateToModel();
            if (sinhVienInfoInput1.Save())
            {
                sinhVienPayment1.SetHocKy_ID((int)comboBoxHocKy1.SelectedValue);
                sinhVienPayment1.SetHocKy_ID((int)comboBoxNamHoc1.SelectedValue);
                sinhVienController.SavePhanNganh(chonNganhHoc1.GetSelectedNganhHoc_ID(), (int)txtKhoaHoc_IDFilter.SelectedValue, true);
                sinhVienController.SaveLoaiGiayTo(loaiGiayToCheckList1.GetData());
                sinhVienPayment1.Save();
                SinhVienTrungTuyenEntity oTrungTuyen = GetObject();
                if (oTrungTuyen.SinhVien_ID == 0)
                {
                    sinhVienPayment1.SaveSoNo();
                }
                oTrungTuyen.NguoiNhap = SystemControllers.UsersController.User.UserName;
                oTrungTuyen.NgayNhap = DateTime.Now;
                oTrungTuyen.SinhVien_ID = sinhVienController.SinhVienObject.SinhVien_ID;
                sinhVienTrungTuyenController.SinhVienTrungTuyenObject = oTrungTuyen;
                sinhVienTrungTuyenController.Update();
                lyLichSinhVienInfo1.UpdateToModel();
                sinhVienController.SaveLyLichSinhVien();
            }

        }

        private void btnPrintPhieuThu_Click(object sender, EventArgs e)
        {
            if (sinhVienController.SoCoSinhVien!=null)
            {
             
            }
        }
    }
}